package com.android.mios.util;

import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class Utils {
	public static boolean isEmpty(String s) {
		return s == null || "null".equalsIgnoreCase(s)
				|| s.trim().length() == 0;
	}

	public static String intToHex(int value, int hexLen) {
		String hex = Integer.toHexString(value);
		while (hex.length() < hexLen) {
			hex = "0" + hex;
		}
		return hex;
	}

	public static String ipToHex(String ip) {
		StringBuffer hex = new StringBuffer();
		String[] parts = ip.split("\\.");
		for (String part : parts) {
			String partHex = intToHex(Integer.parseInt(part), 2);
			hex.append(partHex);
		}
		return hex.toString();
	}

	public static Bitmap resizeBitmap(Bitmap bm, float scale) {
		int width = bm.getWidth();
		int height = bm.getHeight();

		int newWidth = (int) ((float) width * scale);
		int newHeight = (int) ((float) height * scale);

		Matrix matrix = new Matrix();
		matrix.postScale(scale, scale);

		Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, newWidth,
				newHeight, matrix, false);

		return resizedBitmap;
	}

	private static final Pattern emailPattern = Pattern
			.compile(".+@.+\\.[a-z]+");

	public static boolean isValidEmail(String email) {
		Matcher m = emailPattern.matcher(email);
		return m.matches();
	}

	public static String stringToMD5Hex(String s) {
		try {
			byte[] defaultBytes = s.getBytes();
			MessageDigest algorithm = MessageDigest.getInstance("MD5");
			algorithm.reset();
			algorithm.update(defaultBytes);
			byte messageDigest[] = algorithm.digest();
			return bytesToHex(messageDigest);
		} catch (Exception e) {
		}
		return "";
	}

	public static String bytesToHex(byte[] a) {
		StringBuilder sb = new StringBuilder(a.length * 2);
		for (int i = 0; i < a.length; i++) {
			sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
			sb.append(Character.forDigit(a[i] & 0x0f, 16));
		}
		return sb.toString();
	}

	public static String stringToHex(String s) {
		return bytesToHex(s.getBytes());
	}

	// Drawableת��BitMap
	public static Bitmap drawableToBitmapByBD(Drawable drawable) {
		BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
		return bitmapDrawable.getBitmap();
	}

	public static Drawable bitmapToDrawableByBD(Bitmap bitmap) {
		Drawable drawable = new BitmapDrawable(bitmap);
		return drawable;
	}

	public static Bitmap createBitmapBySize(Bitmap bitmap, int width, int height) {
		return Bitmap.createScaledBitmap(bitmap, width, height, false);
	}

	public static String getPhotoFileName(String name, int number, int id) {
		// Date date = new Date(System.currentTimeMillis());
		// SimpleDateFormat dateFormat = new SimpleDateFormat(
		// "'IMG'_yyyy-MM-dd HH:mm:ss");
		// return dateFormat.format(date) + ".jpg";
		return name + "_" + number + "_" + id+ ".jpg";
	}

}